import API from '../../utils/api'
import common from '../../utils/common'
const date = new Date()
const years = []
const months = []
const days = []
const hour = []
const min = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  i = common.mendZero(i)
 
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  i = common.mendZero(i)
  days.push(i)
}
for (let i = 1; i <= 24; i++) {
  i = common.mendZero(i)
  hour.push(i)
}
for (let i = 1; i <= 60; i++) {
  i = common.mendZero(i)
  min.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    starTime: '',
    endTime: '',
    priceNum: 0,
    region: ['请选择'],
    customItem: '全部',
    isChecked: true,
    imgFile: '../../images/haibaobg.png',
    store_name: '',
    years: years,
    year: date.getFullYear(),
    months: months,
    month: '02',
    days: days,
    day: '02',
    value: [2019, 2, 1],
    hours: hour,
    mins: min,
    min: '01',
    hour: '01',
    isShowDate: false,
    isShowDateMask: false,
    endDateType: 1,
    year2: date.getFullYear(),
    month2: '02',
    day2: '02',
    min2: '01',
    hour2: '01',
    isUpPic: false,
    setMoney: '',
    picList: [],
    showList: {},
    address: '',
    pcate_name:'',
    isEdit:false,
    phone:''
  },
  onLoad: function (options) {
    console.log(options)
    this.getNowTime()

  },
  getNowTime (time) {
    console.log(common.dateFormat(time,2))
    var date = common.dateFormat(time,2)

    var y = date.substr(0,4)
    var m = date.substr(5,2)
    var today = date.substr(8,2)
    var hour = date.substr(11,2)
    var min = date.substr(14,2)
    console.log(y)
    this.setData({
      year:y,
      month:m,
      day:today,
      hour:hour,
      min:min,
      year2:y,
      month2:m,
      day2:today,
      hour2:hour,
      min2:min,
      value:[y,m-1,today-1,hour-1,min-1],
      starTime:date,
      endTime:date

    })
  },
  userMobile (e) {
    wx.setStorageSync('phone',e.detail.value)
    this.setData({
      phone:e.detail.value
    })
  },
  // 获取详情页数据
  getAcivityDetailData(id) {
    var data = {
      id: id
    }
    API.toActivityDetail(data).then(res => {
      if (res.code == 0) {
        console.log(res.data.actInfo[0].image)
        var actInfo = res.data.actInfo[0]
        var list = JSON.parse(actInfo.description)
        var showList = { content: list[0].content, imgUrl: list[0].imgUrl }
        wx.setStorageSync('setMoney', actInfo.price_name)
        wx.setStorageSync('pcate_id', actInfo.pcate_id)
        wx.setStorageSync('cate_id', actInfo.cate_id)
        wx.setStorageSync('activityDetailPic')
        wx.setStorageSync('address', actInfo.address)
        wx.setStorageSync('latitude', actInfo.latitude)
        wx.setStorageSync('longitude', actInfo.longitude)
        wx.setStorageSync('pcate_name',actInfo.pcate_name)
        wx.setStorageSync('phone',actInfo.phone)
        this.editTimeDeal(actInfo.startime,actInfo.endtime)
        this.setData({
          imgFile: actInfo.image,
          isUpPic: true,
          store_name: actInfo.store_name,
          picList: list,
          showList: showList,
          address: actInfo.address,
          setMoney: actInfo.price_name,
          pcate_name:actInfo.pcate_name,
          phone:actInfo.phone
        })
      }
    })
  },
  editTimeDeal (time1,time2) {
    var dateStrar = common.dateFormat(time1,2)
    var dateEnd = common.dateFormat(time2,2)
    var y = dateStrar.substr(0,4)
    var m = dateStrar.substr(5,2)
    var today = dateStrar.substr(8,2)
    var hour = dateStrar.substr(11,2)
    var min = dateStrar.substr(14,2)
    var yEnd = dateEnd.substr(0,4)
    var mEnd = dateEnd.substr(5,2)
    var todayEnd = dateEnd.substr(8,2)
    var hourEnd = dateEnd.substr(11,2)
    var minEnd = dateEnd.substr(14,2)
    console.log(y)
    this.setData({
      year:y,
      month:m,
      day:today,
      hour:hour,
      min:min,
      year2:yEnd,
      month2:mEnd,
      day2:todayEnd,
      hour2:hourEnd,
      min2:minEnd,
      value:[y,m-1,today-1,hour-1,min-1],
      starTime:dateStrar,
      endTime:dateEnd

    })
  },
  /*-- 图片上传 --*/
  fileUp() {
    var that = this
    wx.uploadFile({
      url: 'https://lequer.odeman.com/weixin/Decaler/upload_images?productid=' + wx.getStorageSync('product_id'),
      filePath: wx.getStorageSync('UpPic'),
      name: 'file',
      formData: {
        pwd: 9999
      },
      success(res) {
        var data = JSON.parse(res.data)
        if (data.code == 0) {
          that.setData({
            isUpPic: true,
            imgFile: wx.getStorageSync('UpPic')

          })
        }

      }
    })
  },
  // 获取标题 
  storeName(e) {
    wx.setStorageSync('store_name',e.detail.value)
    this.setData({
      store_name: e.detail.value
    })
  },
  // 获取主体内容
  description(e) {
    this.setData({
      description: e.detail.value
    })
  },
  // 获取用户上传的活动详情图片
  getDescription() {
    var data = {
      productid: wx.getStorageSync('product_id')
    }
    API.getDescription(data).then(res => {
      if (res.code == 0 && res.description != "") {
        var list = JSON.parse(res.description)
        var showList = { content: list[0].content, imgUrl: list[0].imgUrl }
        this.setData({
          picList: list,
          showList: showList
        })
      }
    })
  },
  addActivityDetail() {
    wx.navigateTo({
      url: '/pages/activityDetailDescribe/activityDetailDescribe'
    })
  },
  // 发布活动
  publicActivity() {
    if (this.data.isUpPic == false) {
      wx.showModal({
        title: '',
        content: '请上传活动海报',
      })
      return
    }
    if (this.data.store_name == '') {
      wx.showModal({
        title: '',
        content: '请填写活动标题',
      })
      return
    }
    if (!wx.getStorageSync('pcate_id')) {
      wx.showModal({
        title: '',
        content: '请选择分类',
      })
      return
    }
    if (!wx.getStorageSync('cate_id')) {
      wx.showModal({
        title: '',
        content: '请选择分类',
      })
      return
    }
    if (!wx.getStorageSync('setMoney')) {
      wx.showModal({
        title: '',
        content: '请设置费用并添加票种',
      })
      return
    }
    if (this.data.picList.length == 0) {
      wx.showModal({
        title: '',
        content: '请添加活动描述',
      })
      return
    }

    if (!wx.getStorageSync('address')) {
      wx.showModal({
        title: '',
        content: '请选择地点',
      })
      return
    }
    var regex = /^1[2|3|4|5|6|7|8|9]\d{9}$/
    if (!regex.test(this.data.phone)) {
      wx.showModal({
        title: '',
        content: '请输入正确的手机号',
      })
      return
    }
    var data = {
      productid: wx.getStorageSync('product_id'),
      store_name: this.data.store_name,
      startime: this.data.starTime,
      endtime: this.data.endTime,
      address: wx.getStorageSync('address'),
      latitude: wx.getStorageSync('latitude'),
      longitude: wx.getStorageSync('longitude'),
      city: wx.getStorageSync('childid'),
      cate_id: wx.getStorageSync('cate_id'),
      pcate_id: wx.getStorageSync('pcate_id'),
      price_name: wx.getStorageSync('setMoney'),
      pcate_name:wx.getStorageSync('pcate_name'),
      phone:wx.getStorageSync('phone'),
    }
    // 如果来自活动编辑就用更新按钮
    if (wx.getStorageSync('editproduct_id')) {
      API.updateActive(data).then(res => {
        if (res.code == 0) {
          wx.showToast({
            title: '更新成功',
            icon: 'success',
            duration: 2000
          })
          wx.removeStorageSync('setMoney')
          wx.removeStorageSync('pcate_id')
          wx.removeStorageSync('cate_id')
          wx.removeStorageSync('activityDetailPic')
          wx.removeStorageSync('product_id')
          wx.removeStorageSync('address')
          wx.removeStorageSync('latitude')
          wx.removeStorageSync('longitude')
          wx.removeStorageSync('pcate_name')
          wx.removeStorageSync('UpPic')
          wx.removeStorageSync('store_name')
          wx.removeStorageSync('pcate_name')
          wx.removeStorageSync('phone')
          setTimeout(res => {
            wx.switchTab({
              url: '/pages/index/index'
            })
          }, 2000)
  
        } 
        //else {

        //   wx.showModal({
        //     title: '',
        //     content: res.error_msg
        //   })
        // }
      })
    } else {
      API.saveActive(data).then(res => {
        if (res.code == 0) {
          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 2000
          })
          wx.removeStorageSync('setMoney')
          wx.removeStorageSync('pcate_id')
          wx.removeStorageSync('cate_id')
          wx.removeStorageSync('activityDetailPic')
          wx.removeStorageSync('product_id')
          wx.removeStorageSync('address')
          wx.removeStorageSync('latitude')
          wx.removeStorageSync('longitude')
          wx.removeStorageSync('pcate_name')
          wx.removeStorageSync('UpPic')
          wx.removeStorageSync('store_name')
          wx.removeStorageSync('pcate_name')
          wx.removeStorageSync('phone')
          setTimeout(res => {
            wx.switchTab({
              url: '/pages/index/index'
            })
          }, 2000)
  
        } 
        // else {
        //   wx.showModal({
        //     title: '',
        //     content: res.error_msg
        //   })
        // }
      })
    }
   
  },
  /*-- 开始日期选择 --*/
  starDateChange(e) {
    this.setData({
      starDate: e.detail.value
    })
  },
  /*-- 开始时间选择 --*/
  starTimeChange(e) {
    this.setData({
      starTime: e.detail.value
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      imgFile:'../../images/haibaobg.png',
      store_name:'',
      setMoney: '',
      address:'',
      pcate_name:'',
      picList: [],
      isEdit:false


    })
    if (wx.getStorageSync('store_name')) {
      this.setData({
        store_name:wx.getStorageSync('store_name')
      })
    }

    console.log(wx.getStorageSync('pcate_name'))
    if (wx.getStorageSync('pcate_name')) {
      this.setData({pcate_name:wx.getStorageSync('pcate_name')})
    }
    if (wx.getStorageSync('phone')) {
      this.setData({phone:wx.getStorageSync('phone')})
    }
    if (wx.getStorageSync('editproduct_id')) {
      console.log('存在')
      this.getAcivityDetailData(wx.getStorageSync('editproduct_id'))
      wx.setStorageSync('product_id', wx.getStorageSync('editproduct_id'))
      this.setData({
        isEdit:true
      })

    } else {
      console.log('不存在')
      this.getActivityId()
    }
    if (wx.getStorageSync('setMoney')) {
      this.setData({
        setMoney: wx.getStorageSync('setMoney')
      })
    }
    if (wx.getStorageSync('activityDetailPic')) {
      this.getDescription()
    }
    if (wx.getStorageSync('UpPic')) {
      this.fileUp()
    }
    if (wx.getStorageSync('address')) {
      this.setData({
        address: wx.getStorageSync('address')
      })
    }

  },
  selectAddress() {
    wx.chooseLocation({
      success: function (res) {
        wx.setStorageSync('address', res.address)
        wx.setStorageSync('latitude', res.latitude)
        wx.setStorageSync('longitude', res.longitude)
      },
      fail: function (err) {

      },
      complete: function (res) {
  
      }
    })
  },
  tofileUp() {
    wx.navigateTo({
      url: '/pages/cutInside/cutInside?type=1'
    })
  },
  // 获取活动 id
  getActivityId() {
    var data = {
      token: wx.getStorageSync('token')
    }
    API.addActive(data).then(res => {
      wx.setStorageSync('product_id', res.data.product_id)
    })
  },

  //选择开始日期
  startDate() {
    console.log('执行了开始')
    this.setData({
      isShowDate: true,
      isShowDateMask: true,
      endDateType: 1
    })
  },
  // 选择结束日期
  endDate() {
    console.log('扫行了结束')
    this.setData({
      isShowDate: true,
      isShowDateMask: true,
      endDateType: 2
    })
  },
  // 关闭日期弹框
  cancelSelectDate() {
    this.setData({
      isShowDate: false,
      isShowDateMask: false
    })
  },
  // 关闭日期弹框
  doneSelectDate() {
    this.setData({
      isShowDate: false,
      isShowDateMask: false
    })
  },
  bindChange: function (e) {
    console.log(this.data.endDateType)
    const val = e.detail.value
    if (this.data.endDateType == 1) {
      console.log('开始时间')
      this.setData({
        year: this.data.years[val[0]],
        month: this.data.months[val[1]],
        day: this.data.days[val[2]],
        hour: this.data.hours[val[3]],
        min: this.data.mins[val[4]],
        starTime: this.data.years[val[0]] + '-' + this.data.months[val[1]] + '-' + this.data.days[val[2]] + ' ' + this.data.hours[val[3]] + ':' + this.data.mins[val[4]]
      })
      console.log(this.data.starTime)
    } else {
      console.log('结束时间')
      this.setData({
        year2: this.data.years[val[0]],
        month2: this.data.months[val[1]],
        day2: this.data.days[val[2]],
        hour2: this.data.hours[val[3]],
        min2: this.data.mins[val[4]],
        endTime: this.data.years[val[0]] + '-' + this.data.months[val[1]] + '-' + this.data.days[val[2]] + ' ' + this.data.hours[val[3]] + ':' + this.data.mins[val[4]]
      })
    }

  }
})